package JZOffer;

import java.util.Stack;

public class JzOffer31 {
    public boolean validateStackSequences(int[] pushed, int[] popped) {
        Stack<Integer> stack = new Stack<>();
        int index = 0;
        for (int x : pushed) {
            stack.add(x);
            while (!stack.isEmpty() && stack.peek() == popped[index]) {
                stack.pop();
                index++;
            }
        }
        return stack.isEmpty();
    }

    public static void main(String[] args) {
        JzOffer31 jzOffer31 = new JzOffer31();
        int[] pushed = {1,2,3,4,5};
        int[] popped = {4,3,5,1,2};

        System.out.println(jzOffer31.validateStackSequences(pushed, popped));
    }
}
